package online.heycm.platform.web.common.sse;

import online.heycm.platform.common.entity.result.Result;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * @author hey
 * @version 1.0
 * @date 2023/12/18 15:39
 */
public class SseServiceImpl implements SseService {
    @Override
    public SseEmitter connect(String id) {
        return SseServer.connect(id).getSseEmitter();
    }

    @Override
    public Result send(String id) {
        final SseSession session = SseServer.getSession(id);
        if (session == null) {
            return Result.error("SSE 已下线");
        }
        session.send("发送内容");
        return Result.success();
    }

    @Override
    public Result close(String id) {
        final SseSession session = SseServer.getSession(id);
        if (session != null) {
            session.close();
        }
        return Result.success();
    }
}
